package com.zcl.controller;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.zcl.dao.User;
import com.zcl.service.UserService;
import com.zcl.util.GetCurrentUserDetails;

@Controller
public class UserController {
	Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	UserService userService;
	/**
	 * 获取当前用户所拥有的权限
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/user/authorites")
	public Collection<? extends GrantedAuthority> userAuthorities(){
		Collection<? extends GrantedAuthority> authorities = GetCurrentUserDetails.currentUser().getAuthorities();
		return authorities;
	}
	
	/**
	 * 用户个人资料页面
	 */
	@RequestMapping("/userPage")
	public String userPage(Model model) {
		String userName = GetCurrentUserDetails.currentUser().getUsername();
		User user = userService.userQueryByName(userName);
		logger.info("用户个人资料页面"+user.getUsername());
		model.addAttribute("user", user);
		return "user";
	}
	
	/**
	 * 用户个人资料页面2
	 */
	@ResponseBody
	@RequestMapping("/userPage2")
	public User userPage2() {
		String userName = GetCurrentUserDetails.currentUser().getUsername();
		User user = userService.userQueryByName(userName);
		logger.info("用户个人资料页面2"+user.getUsername());
		return user;
	}
	/**
	 * 用户信息修改
	 */
	@RequestMapping("/user/changeUser")
	public void changeUser(HttpServletRequest request) {
		Map<String, String> map = new HashMap<>();
		map.put("userId", request.getParameter("userId"));
		map.put("userName", request.getParameter("username"));
		map.put("province", request.getParameter("province"));
		map.put("city", request.getParameter("city"));
		map.put("area", request.getParameter("area"));
		map.put("email", request.getParameter("email"));
		map.put("other", request.getParameter("other"));
		userService.changeUser(map);
	}
	@RequestMapping("/changePwdPage")
	public String userChangePwd(Model model) {
		String userName = GetCurrentUserDetails.currentUser().getUsername();
		User user = userService.userQueryByName(userName);
		model.addAttribute("userName", userName);
		model.addAttribute("userId", user.getUserId());
		return "changePwd";
	}
	@ResponseBody
	@RequestMapping("/user/getPwd")
	public String getPwd() {
		return GetCurrentUserDetails.currentUser().getPassword();
	}
	
	@RequestMapping("/user/changePwd")
	public void userChangPwd(HttpServletRequest request) {
		Map<String, String> map = new HashMap<>();
		map.put("userId", request.getParameter("userId"));
		map.put("password", request.getParameter("newPwd"));
		userService.changePwd(map);
	}
	
	/**
	 * 上传文件：
	 * 1、保存文件至项目/Tomcat路径下
	 * 2、文件路径保存到数据库中
	 */
	@ResponseBody
	@RequestMapping("/user/changeIcon")
	public String familyEditIcon(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
	    JSONObject result = new JSONObject();
	    if (file.isEmpty()) {
			result.put("result", "文件为空");
			return result.toJSONString();
	    }
	    String fileName = file.getOriginalFilename();
	    String path = "";
	    File folderFile = new File("C:\\Users\\yinyunqi\\git\\marac\\src\\main\\webapp\\WEB-INF\\headSculpture");
	    String iconPath = null;
	    try {
	    	iconPath = folderFile.getCanonicalPath();
	    } catch (IOException e1) {
	    	e1.printStackTrace();
	    }
	    String randomPath = UUID.randomUUID().toString();
	    String filePath = iconPath+"\\"+randomPath+fileName;
	    File dest = new File(filePath);
	    if (!dest.getParentFile().exists()) {
	    	dest.getParentFile().mkdirs();
	    }
	    try {
			file.transferTo(dest);
			result.put("result", "文件上传成功");
//			//将路径保存到数据库
			Map<String, String> map = new HashMap<String, String>();
			map.put("iconUrl", "headSculpture/"+randomPath+fileName);
			map.put("userName",GetCurrentUserDetails.currentUser().getUsername());
			userService.changeIconUrl(map);
			return result.toJSONString();
	    } catch (IllegalStateException e) {
	    	e.printStackTrace();
	    }catch (IOException e) {
	    	e.printStackTrace();
	    }
	    result.put("result", "文件上传失败");
	    return result.toJSONString();
	}
}
